#include <iostream>
#include <vector>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
private:
    int res = 0;

public:
    void Sum(TreeNode *root, int tmpSum)
    {
        if (root == nullptr)
            return;
        if (tmpSum == root->val)
            res++;
        Sum(root->left, tmpSum - root->val);
        Sum(root->right, tmpSum - root->val);
    }
    int pathSum(TreeNode *root, int targetSum)
    {
        if (root == nullptr)
            return 0;
        Sum(root, targetSum);
        pathSum(root->left, targetSum);
        pathSum(root->right, targetSum);
        return res;
    }
};